<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register extends MY_Controller {

  	
    function __construct()
    {
        parent::__construct();
		
    }
	
	public function geotest()
	{
		$this->load->view('geotest');
	}

	public function index()
	{	
		if($this->Register_model->userIsRegistered() === true){
			redirect('/play');
		}
		
		$this->data['title'] .= 'Test'; // extend title set in MY_controller
		$this->setupRegisterForm();
		$this->setupValidationRules();
		$this->data['form'] = buildForm($this->formElements);
 		$this->data['headingClass'] = 'we-need-some-data';
		$this->data['heading'] = 'Before we can serve your tea or coffee we need some data ';
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('forms/register', $this->data);
		}
		else
		{
			$sResult = $this->registerUser();
			$this->setIdCookie($sResult);
		
			// forward somewhere
			redirect('/play');
		}
		
	}
	
	public function setIdCookie($value)
	{
		$this->input->set_cookie('drinkerId', $value, '315725000');
	}
	
	public function registerUser()
	{
		
		$name = $this->input->post('name');
		$email = $this->input->post('email');
		$location = $this->input->post('location');
		$beverage = $this->input->post('beverage');
		$sugars = $this->input->post('sugars');
		$milk = $this->input->post('milk');
		$office = $this->input->post('office');
		
		$oResult = $this->Register_model->register($name, $email, $location, $beverage, $sugars, $milk, $office);
		return $oResult;
	}

	
	public function setupRegisterForm()
	{
	 	$this->formElements = array(
			array(
				'type'		  => 'text',
				'name'        => 'name',
              	'id'          => 'name',
              	'value'       => $this->input->post('name',TRUE),
              	'data-rules'        => 'required',
              	'class'       => 'form-input',	
				),
			array(
				'type'		  => 'text',
				'name'        => 'email',
              	'id'          => 'email',
              	'value'       => $this->input->post('email',TRUE),
              	'data-rules'  => 'required|valid_email',
              	'class'       => 'form-input',	
				),
			array(
				'type'		  => 'select',
				'name'        => 'office',
              	'id'          => 'office',
              	'data-rules'  => 'required',
              	'class'      => 'form-select',	
				'options'	=> array(''=>'Select', 'ROSEBURY'=>'ROSEBURY', 'SJS'=>'SJS')
				),
				/*
			array(
				'type'		  => 'select',
				'name'        => 'beverage',
              	'id'          => 'beverage',
              	'data-rules'  => 'required',
              	'class'      => 'form-select',	
				'options'	=> array('Tea', 'Coffee')
				),
			array(
				'type'		  => 'text',
				'name'        => 'sugars',
              	'id'          => 'sugars',
              	'value'       => ($this->input->post('sugars',TRUE) ? $this->input->post('sugars',TRUE) : 0 ),
              	'data-rules'  => 'required|numeric',
              	'class'       => 'form-input',
				),
			array(
				'type'		  => 'select',
				'name'        => 'milk',
              	'id'          => 'milk',
              	'data-rules'  => 'required',
              	'class'      => 'form-select',	
				'options'	=> array('Yes', 'No')
				),	
			*/
			'submit' => array(
				'type'		  => 'submit',
				'name'        => 'submit',
              	'id'          => 'submit',
              	'value'       => '',
				'content'=>'Submit',
              	'class'       => 'button submit',
				)
		);

	}
	
	

	
	public function setupValidationRules()
	{
 		foreach ($this->formElements as $el) {
			if(!isset($el['data-rules'])) continue;

 			$this->form_validation->set_rules(
				$el['name'], ucfirst(str_replace('_', ' ', $el['name'])), $el['data-rules']
			);
 		}
		
	}
	
	
	function test(){
		
		echo __FUNCTION__;
		
		//load reg model
		$this->load->model('Register_model');
		
		//
		$name = "Anthony";
		$email = "anthony.galvin@rga.com";
		$location = "4th Floor near the windows";
		$beverage = "tea";
		$sugars = 0;
		$milk = false;
		$office = "SJS";
		
		$oResult = $this->Register_model->register($name, $email, $location, $beverage, $sugars, $milk, $office);
		
		echo $oResult;
	
	}
	
	function play(){
		$this->load->view('pages/play');
	}
	
}
 

/* End of file Register.php */
/* Location: ./application/controllers/welcome.php */